package com.android.lovely.ui.dialog

import android.os.Build
import android.util.Log
import android.widget.NumberPicker
import androidx.fragment.app.activityViewModels
import com.android.lovely.R
import com.android.lovely.databinding.DialogTransTimeBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.ui.viewmodel.TransViewModel
import com.android.lovely.ui.viewmodel.WorkCreateViewModel
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/13 18:55
 */
class CreateRoleTypeDialog : BaseDialogFragment<DialogTransTimeBinding>() {
    val viewModel by activityViewModels<WorkCreateViewModel>()

    var trans = 0
    var type = 0

    private val roleTabsType by lazy {
        arrayOf(getString(R.string.all), getString(R.string.in_review), getString(R.string.be_rejected), getString(
            R.string.passed))
    }

    private val roleLocalTabsType by lazy {
        arrayOf(getString(R.string.all))
    }

    private val roleTabs by lazy {
        arrayOf(getString(R.string.public_desc), getString(R.string.private_desc), getString(R.string.draft))
    }

    override fun initializeViews() {
        trans = viewModel.roleType.value?.first ?: 0
        type = viewModel.roleType.value?.second ?: 0
        binding.tvTitle.text = getString(R.string.select_type)
        updateRight(type)

        binding.mainWheelLeft.apply {
            // 配置数值范围
            setDisplayedValues(roleTabs)
            setWrapSelectorWheel(false)
            minValue = 0
            maxValue = roleTabs.size - 1
            hideNumberPickerDivider()
            value = trans // 默认值
            scrollBarSize = 0
            setFormatter { value ->
                value.toString().padStart(2, '0')
            }
// 监听数值变化
            setOnValueChangedListener { _, oldVal, newVal ->
                Log.d("Picker", "值变化：$oldVal -> $newVal")
                trans = newVal
                updateRight(0)
            }
        }

        binding.ivClose.clickable {
            dismissAllowingStateLoss()
        }
        binding.confirmBtn.clickable {
            viewModel.updateRole(trans, type)
            dismissAllowingStateLoss()
        }
    }

    private fun updateRight(type: Int) {
        binding.mainWheelRight.apply {
            minValue = 0
//            value = type // 默认值
            // 配置数值范围
            if(trans == 2) {
                maxValue = roleLocalTabsType.size - 1
                setDisplayedValues (roleLocalTabsType)

            } else {
                setDisplayedValues(roleTabsType)
                maxValue = roleTabsType.size - 1
            }
            setWrapSelectorWheel(false)
            hideNumberPickerDivider()
            scrollBarSize = 0
            value = type // 默认值

            setFormatter { value ->
                value.toString().padStart(2, '0')
            }
            // 监听数值变化
            setOnValueChangedListener { _, oldVal, newVal ->
                Log.d("Picker", "值变化：$oldVal -> $newVal")
                this@CreateRoleTypeDialog.type = newVal
            }
        }
    }

    override fun isBottom(): Boolean {
        return true
    }

}

